package zhongbo.java.air.monitor.we;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import zhongbo.java.air.monitor.listener.RedisLogChanelPublisher;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@ServerEndpoint("/websocket/live")
@Component
public class WebsocketAir {
    public static Map<String, WebsocketAir> WeAirs = new ConcurrentHashMap<>();
    public Session session;
    private RedisLogChanelPublisher redisLogChanelPublisher;

    @Autowired
    public void setServices(
            RedisLogChanelPublisher mps) {
        redisLogChanelPublisher = mps;
    }

    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        WeAirs.put(session.getId(), this);
        System.out.println("有新连接");
    }

    @OnMessage
    public void onMessage(String message) {


    }

    @OnClose
    public void onClose(Session session) {
        WeAirs.remove(session.getId());
    }

    @OnError
    public void onError(Throwable error) {
        System.out.println("发生错误：" + error.getMessage());
    }
}
